package Crawl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class GetThread extends Thread {

    private final CloseableHttpClient httpClient;
    private final HttpContext context;
    private final HttpGet httpget;
    String url="http://cec.jmu.edu.cn/";     //复习的时候看到了可以用final定义变量，这样可以减少开销
    ArrayList lis=new ArrayList();
    ArrayList lis1=new ArrayList();
    public GetThread(CloseableHttpClient httpClient, HttpGet httpget) {
        this.httpClient = httpClient;
        this.context = HttpClientContext.create();
        this.httpget = httpget;
    }

	public ArrayList getLis() {
		return lis;
	}

	public void setLis(ArrayList lis) {
		this.lis = lis;
	}

	public ArrayList getLis1() {
		return lis1;
	}
	//list1����ֱ�Ӵ�50��url������ȡ�õ��ģ�Ȼ��ֱ�ȥ������Щ����������˳����Щ�����

	public void setLis1(ArrayList lis1) {
		this.lis1 = lis1;
	}

	@Override
    public void run() {
        try {
            CloseableHttpResponse response = httpClient.execute(httpget, context);
            try {
                HttpEntity entity = response.getEntity();
                Document doc=Jsoup.parse(EntityUtils.toString(entity,"utf8"));
                Elements links=doc.select(".c124907");    
                for (Element link : links) {  
                	
//                    System.out.println(url +link.attr("href"));  
                    lis1.add(url +link.attr("href"));
                }  
                
      
//                String pattern="\\?a2t=\\d.*&a2p=\\d.*&a2c=\\d.*\\d";   
//                String pattern="\\?a2t=([0-9]{1,}).*&a2p=\\d.*&a2c=10&urltype=tree.TreeTempUrl&wbtreeid=([0-9]{1,})";   
                String pattern ="\\?a2t=([0-9]{1,})&a2p=[0-9]{1,}&a2c=10&urltype=tree.TreeTempUrl&wbtreeid=([0-9]{1,})";  
                Elements links1=doc.select("a[href]"); 
                for (Element link1 : links1) {
                	String line=link1.attr("href");
                	Pattern r = Pattern.compile(pattern);
                	Matcher m = r.matcher(line);
                	int i=0;
                	if (m.find( )) {
//                		 System.out.println("Found value: " + m.group(0) );
                		int j=Integer.parseInt(m.group(1));
                 		if(j>7){
                 			for(int k=1;k<j+1;k++){
                 				lis.add("?a2t="+String.valueOf(j)+"&a2p="+String.valueOf(k)+"&a2c=10&urltype=tree.TreeTempUrl&wbtreeid="+m.group(2));
                 				}
                 			}
                 		else{
                		lis.add(m.group(0));
                 		}
                		
                		
                		//其实在这边 的话还是有些不够成熟，碰到大于8页的就是不太好弄了
                		
                		//���lis�Ļ����Ǵ��Ǹ���ҳ�ĵط�����ӵ�

                	}
                 else {
                }
                		
				}
                
            } finally {
                response.close();
                 
            }
        } catch (ClientProtocolException ex) {
        } catch (IOException ex) {
        }
    }

}
 

